<?php

namespace App\Http\Validate\Activity;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Activity\ShopOrderActivityModel;
use Illuminate\Support\Carbon;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ShopOrderActivityValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * 列表校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function listValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "pageSize",
                "pageNum",
                "name",
                "status",
                "createdByUid",
                "type",
                "shopId"
            ],
            $params,
            [
                "pageSize"     => "required|int|min:1|max:100",
                "pageNum"      => "required|int|min:1",
                "name"         => "sometimes|string|min:1",
                "status"       => [
                    "sometimes",
                    "int",
                    "in:" . implode(",", array_keys(ShopOrderActivityModel::STATUS_TEXT))
                ],
                "type"         => [
                    "sometimes",
                    "int",
                    "in:" . implode(",", array_keys(ShopOrderActivityModel::TYPE_TEXT))
                ],
                "createdByUid" => "sometimes|int|min:1",
                "shopId"       => "sometimes|int|min:1",
                "startTime"    => "required_with:endTime|date_format:Y-m-d",
                "endTime"      => "required_with:startTime|date_format:Y-m-d",
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function dataStatisticsValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "id",
                "pageSize",
                "pageNum",
                "name",
                "status",
                "createdByUid",
                "type",
                "shopId",
                "startTime",
                "endTime"
            ],
            $params,
            [
                "pageSize"     => "required|int|min:1|max:100",
                "pageNum"      => "required|int|min:1",
                "id"           => [
                    "sometimes",
                    "int",
                    Rule::exists((new ShopOrderActivityModel())->getTable(), "id")->where("isDeleted", 0)
                ],
                "name"         => "sometimes|string|min:1",
                "status"       => [
                    "sometimes",
                    "int",
                    "in:" . implode(",", array_keys(ShopOrderActivityModel::STATUS_TEXT))
                ],
                "type"         => [
                    "sometimes",
                    "int",
                    "in:" . implode(",", array_keys(ShopOrderActivityModel::TYPE_TEXT))
                ],
                "createdByUid" => "sometimes|int|min:1",
                "shopId"       => "sometimes|int|min:1",
                "startTime"    => "required_with:endTime|date_format:Y-m-d",
                "endTime"      => "required_with:startTime|date_format:Y-m-d",
            ]
        );
    }

    /**
     * 数据统计分类门店列表
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function dataStatisticsShopListValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "businessType",
                "id",
                "pageSize",
                "pageNum",
                "export",
                "shopId"
            ],
            $params,
            [
                "businessType" => self::intInValidateBuild(true, ShopOrderActivityModel::BUSINESS_TYPE_TEXT),
                "id"           => [
                    "required",
                    "int",
                    Rule::exists((new ShopOrderActivityModel())->getTable(), "id")
                ],
                "pageSize"     => "exclude_unless:export,0|required|int|min:1|max:100",
                "pageNum"      => "exclude_unless:export,0|required|int|min:1",
                "export"       => "required|int|in:0,1",
                "shopId"       => "sometimes|int|min:1"
            ]
        );
    }

    /**
     * 创建校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function insertValidate(array $params)
    {
        return self::getInstance()->validate(
            ["type", "name", "expireType", "startTime", "endTime", "intro", "title", "detail", "shopType", "remark"],
            $params,
            [
                "type"       => self::intInValidateBuild(true, ShopOrderActivityModel::TYPE_TEXT),
                "name"       => self::stringValidateBuild(true, 120, 1),
                "expireType" => self::intInValidateBuild(true, ShopOrderActivityModel::EXPIRE_TYPE_TEXT),
                "startTime"  => "exclude_unless:expireType," . ShopOrderActivityModel::EXPIRE_TYPE_1 . "|required|date_format:Y-m-d",
                "endTime"    => "exclude_unless:expireType," . ShopOrderActivityModel::EXPIRE_TYPE_1 . "|required|date_format:Y-m-d|after_or_equal:" . Carbon::now()
                                                                                                                                                             ->startOfDay(),
                "intro"      => self::stringValidateBuild(true, 255, 1),
                "title"      => self::stringValidateBuild(true, 120, 1),
                "detail"     => "required|string",
                "shopType"   => self::intInValidateBuild(true, ShopOrderActivityModel::SHOP_TYPE_TEXT),
                "shopIds"    => [
                    "exclude_unless:shopType," . ShopOrderActivityModel::SHOP_TYPE_PART,
                    "required",
                    "array"
                ],
                "shopIds.*"  => [
                    "int"
                ],
                "remark"     => "sometimes|string",
            ]
        );
    }

    /**
     * 编辑校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function updateValidate(array $params)
    {
        return self::getInstance()->validate(
            [
                "id",
                "type",
                "name",
                "expireType",
                "startTime",
                "endTime",
                "intro",
                "title",
                "detail",
                "shopType",
                "remark"
            ],
            $params,
            [
                "id"         => [
                    "required",
                    "int",
                    Rule::exists((new ShopOrderActivityModel())->getTable(), "id")
                ],
                "type"       => self::intInValidateBuild(true, ShopOrderActivityModel::TYPE_TEXT),
                "name"       => self::stringValidateBuild(true, 120, 1),
                "expireType" => self::intInValidateBuild(true, ShopOrderActivityModel::EXPIRE_TYPE_TEXT),
                "startTime"  => "exclude_unless:expireType," . ShopOrderActivityModel::EXPIRE_TYPE_1 . "|required|date_format:Y-m-d",
                "endTime"    => "exclude_unless:expireType," . ShopOrderActivityModel::EXPIRE_TYPE_1 . "|required|date_format:Y-m-d|after_or_equal:" . Carbon::now()
                                                                                                                                                             ->startOfDay(),
                "intro"      => self::stringValidateBuild(true, 255, 1),
                "title"      => self::stringValidateBuild(true, 120, 1),
                "detail"     => "required|string",
                "shopType"   => self::intInValidateBuild(true, ShopOrderActivityModel::SHOP_TYPE_TEXT),
                "shopIds"    => [
                    "exclude_unless:shopType," . ShopOrderActivityModel::SHOP_TYPE_PART,
                    "required",
                    "array"
                ],
                "shopIds.*"  => [
                    "int"
                ],
                "remark"     => "sometimes|string",
            ]
        );
    }

    /**
     * 详情校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function detailValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id"],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new ShopOrderActivityModel())->getTable(), "id")->where("isDeleted", 0)
                ]
            ]
        );
    }

    /**
     * 详情校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function statusValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id", "status"],
            $params,
            [
                "id"     => [
                    "required",
                    "int",
                    Rule::exists((new ShopOrderActivityModel())->getTable(), "id")
                        ->where("isDeleted", 0)
                        ->whereIn("status", [
                            ShopOrderActivityModel::STATUS_0,
                            ShopOrderActivityModel::STATUS_1,
                            ShopOrderActivityModel::STATUS_2,
                        ])
                ],
                "status" => [
                    "required",
                    "int",
                    vsprintf("in:%s,%s", [
                        ShopOrderActivityModel::STATUS_1,
                        ShopOrderActivityModel::STATUS_2,
                    ])
                ]
            ]
        );
    }

    /**
     * 结束校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function finishValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id"],
            $params,
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new ShopOrderActivityModel())->getTable(), "id")
                        ->where("isDeleted", 0)
                        ->whereNotIn("status", [ShopOrderActivityModel::STATUS_3])
                ]
            ]
        );
    }
}
